package com.godofgamblers.hotelmanagementsystem.handler;

import com.godofgamblers.hotelmanagementsystem.pojo.User;
import com.godofgamblers.hotelmanagementsystem.pojo.Worker;
import com.godofgamblers.hotelmanagementsystem.service.WorkingService;
import lombok.Data;
import org.apache.shiro.SecurityUtils;
import org.apache.shiro.session.Session;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

/*

@author huanglang

@create_time 2019-10-22 19:55

Nothing is difficult but bugs

*/
@Data
@Controller
public class WorkingHandler {
    @Autowired
    private WorkingService workingService;

    @RequestMapping("/check_work")
    @ResponseBody
    public String checkWork() {
        System.out.println("工作签到");
        Session session = SecurityUtils.getSubject().getSession();
        Object obj = session.getAttribute("user");
        User user = null;
        if (obj != null) {
            user = (User) obj;
        }
        Worker worker = workingService.checkTodayWorker();

        if (worker == null) {
            workingService.checkWork(user);
            return "上班签到成功";
        }else {
            System.out.println(worker.getUser_account());
            System.out.println(user.getUser_account());
            if (worker.getOff_work_time() != null) {
                return "请勿重复签到";
            }
            if (!worker.getUser_account().equals(user.getUser_account())) {
                return "请勿代人签到";
            }
            workingService.checkOffWork(user);
            return "下班签到成功";
        }
    }

}
